<template>
  <div class="app-container">

    <el-card>
      <el-row>
        <el-form ref="queryForm" inline size="small" :model="queryForm">
          <el-form-item label="所属专业" prop="majorId">
            <my-cascader v-model="queryForm.majorId" :options="majorTreeList" :level="2"></my-cascader>
          </el-form-item>
          <el-form-item label="班级名" prop="name">
            <el-input placeholder="请输入班级名" v-model="queryForm.name"></el-input>
          </el-form-item>
          <el-form-item label="辅导员" prop="teacherId">
            <my-cascader v-model="queryForm.teacherId" :options="teacherTreeList" :level="2"></my-cascader>
          </el-form-item>
          <el-form-item label="年级" prop="grade">
            <el-input placeholder="请输入年级" v-model.number="queryForm.grade"></el-input>
          </el-form-item>
          <el-button icon="el-icon-search" size="small" type="primary" @click="onClickSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQueryForm">重置</el-button>
        </el-form>
      </el-row>
      <el-row v-permission="['admin']">
        <el-button icon="el-icon-plus" size="small" type="primary" plain @click="onClickAdd">新增</el-button>
        <el-button icon="el-icon-edit" size="small" type="success" plain @click="onClickEdit()" :disabled="editDisabled">修改</el-button>
        <el-button icon="el-icon-edit" size="small" type="danger" plain>删除</el-button>
      </el-row>
    </el-card>
    <el-table
      v-loading="loading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" label="ID" width="95" prop="id"></el-table-column>
      <el-table-column label="班级名" prop="name" width="300"></el-table-column>
      <el-table-column label="学生人数" prop="number" width="80"></el-table-column>
      <el-table-column label="年级" prop="grade" width="80"></el-table-column>
      <el-table-column label="毕业年份" prop="graduateYear" width="80"></el-table-column>
      <el-table-column label="所属院系" prop="major.dept.name" width="180"></el-table-column>
      <el-table-column label="所属专业" prop="major.name" width="220"></el-table-column>
      <el-table-column label="辅导员" prop="teacher.name" width="100"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)" v-permission="['admin']">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)" v-permission="['admin']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-drawer
      :title="drawer.title"
      :visible.sync="drawer.show"
      :before-close="onDrawerClose"
      size="50%">
      <el-form :rules="formRules" ref="editForm" label-width="auto" :model="form" size="medium">
        <el-form-item label="所属专业" prop="majorId">
          <my-cascader v-model="form.majorId" :options="majorTreeList" :level="2"></my-cascader>
        </el-form-item>
        <el-form-item label="班级名" prop="name">
          <el-input v-model="form.name" placeholder="请输入班级名"></el-input>
        </el-form-item>
        <el-form-item label="辅导员" prop="teacherId">
          <my-cascader v-model="form.teacherId" :options="teacherTreeList" :level="2"></my-cascader>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model.number="form.grade" placeholder="请输入年级"></el-input>
        </el-form-item>
      </el-form>
      <div class="form-drawer-footer">
        <el-button @click="onDrawerClose">取 消</el-button>
        <el-button type="primary" @click="onDrawerConfirm" :loading="drawer.confirmLoading">{{ drawer.confirmLoading ? '提交中 ...' : '确 定' }}</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {addClass, deleteOneClass, getClassList, getOneClass, updateClass} from "@/api/class";
import List from "@/mixins/list";
import {getMajorTreeList, getTeacherTreeList} from "@/api/dept";
import MyCascader from "@/components/MyCascader";
import ClassValidator from "@/validator/ClassValidator";

export default {
  components: {
    MyCascader
  },
  data() {
    return {
      queryListFun: getClassList,
      addFun: addClass,
      updateFun: updateClass,
      queryOneFun: getOneClass,
      deleteOneFun: deleteOneClass,
      validator: ClassValidator,
      queryForm: {
        name: '',
        teacherId: null,
        majorId: null,
        grade: null
      },
      form: {
        name: '',
        number: null,
        teacherId: null,
        majorId: null,
        grade: null
      },
      name: '班级',
      majorTreeList: [],
      teacherTreeList: [],
    }
  },
  mixins: [
    List
  ],
  mounted() {
    getMajorTreeList().then(res => {
      const majorTreeList = []
      for (let i of res.data) {
        const children = []
        for (let j of i.majors) {
          children.push({
            id: j.id,
            value: j.id,
            label: j.name,
          })
        }
        majorTreeList.push({
          id: i.id,
          value: i.id,
          label: i.name,
          children
        })
      }
      this.majorTreeList = majorTreeList
    })
    getTeacherTreeList().then(res => {
      const teacherTreeList = []
      for (let i of res.data) {
        const children = []
        for (let j of i.teachers) {
          children.push({
            id: j.id,
            value: j.id,
            label: j.name,
          })
        }
        teacherTreeList.push({
          id: i.id,
          value: i.id,
          label: i.name,
          children
        })
      }
      this.teacherTreeList = teacherTreeList
    })
  }
}
</script>

<style scoped>

</style>
